package com.xiaoq.matrix.endpoint;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.matrix.entity.QRadioEntity;
import com.xiaoq.matrix.entity.TourismContentEntity;
import com.xiaoq.matrix.entity.TourismMeetingEntity;
import com.xiaoq.matrix.repository.IQRadioRepository;
import com.xiaoq.matrix.service.IQRadioService;
import com.xiaoq.matrix.service.ITourismContentService;
import com.xiaoq.matrix.service.ITourismMeetingService;
import com.xiaoq.matrix.vo.QRadioVo;
import com.xiaoq.matrix.vo.TourismContentVo;
import com.xiaoq.matrix.vo.TourismMeetingVo;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/qradio")
@Api(tags = "小Q电台相关Api")
public class QRadioEndpoint {
	
	@Autowired
	private IQRadioService topTenSer;
	
	@Autowired
	private IQRadioRepository topTenRepository;
	
	@Autowired
	private ITourismContentService tourismContentSer;
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private ITourismMeetingService tourismMeetingSer;
	
	private  class TopTenListResponse extends GenericListResponse<QRadioVo> {}
	
	private static final Logger LOG = LoggerFactory.getLogger(QRadioEndpoint.class);
	
	@ApiOperation(value = "获取小Q电台列表(使用对应的类型和Id去查询详情)", notes = "获取小Q电台列表")
	@ApiResponses({ @ApiResponse(code = 200, response = TopTenListResponse.class, message = "获取小Q电台列表") })
	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> topTenList(
			@RequestParam(value="type",required=true)@ApiParam(value="10资讯文章，20会议")String type,
			@ModelAttribute(value = "pvo") PagingVo pvo) {
		List<QRadioEntity> topTenList = topTenSer.findTopTen(type,pvo);
		List<QRadioVo> topTenVoList = new ArrayList<>();
		QRadioVo topTenVo = null;
		for(QRadioEntity topTen:topTenList){
			topTenVo = new QRadioVo();
			BeanUtils.copyProperties(topTen, topTenVo);
			topTenVoList.add(topTenVo);
		}
		long tc = (long) pvo.getTc();
		return GenericListResponse.listAndCount(topTenVoList, tc);
		
		
	}
	
	@ApiOperation(value = "向小Q电台里面添加新闻资讯或者会议", notes = "向小Q电台里面添加新闻资讯或者会议")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "向小Q电台里面添加新闻资讯或者会议") })
	@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> saveTopTen(@RequestBody QRadioVo topTenVo) {
		QRadioEntity topTen = new QRadioEntity();
		BeanUtils.copyProperties(topTenVo, topTen);
		topTenRepository.save(topTen);
		return GenericResponse.ok();
		
		
	}
	
	@ApiOperation(value = "查询新闻资讯详情", notes = "查询新闻资讯详情")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "查询新闻资讯详情") })
	@GetMapping(value = "/tourismContent", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public  ResponseEntity<?> findTourismContent(@RequestParam(value="id",required=true)@ApiParam(value="文章Id",required=true)String id){
		TourismContentEntity tourismContent = tourismContentSer.findById(id);
		TourismContentVo tourismContentVo = new TourismContentVo();
		BeanUtils.copyProperties(tourismContent, tourismContentVo);
		return GenericDataResponse.okWithData(tourismContentVo);
		
	}
	
	@ApiOperation(value = "对接新闻资讯爬虫", notes = "对接新闻资讯爬虫")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "对接新闻资讯爬虫") })
	@PostMapping(value = "/tourismContent", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public  ResponseEntity<?> savetourismContent(@RequestBody TourismContentVo tourismContentVo){
		
		TourismContentEntity tourismContent = new TourismContentEntity();
		BeanUtils.copyProperties(tourismContentVo, tourismContent);
		tourismContent.setId(idGenerator.nextId());
		tourismContentSer.save(tourismContent);
		return GenericResponse.ok();
		
	}
	
	@ApiOperation(value = "查询旅游会议详情", notes = "查询旅游会议详情")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "查询旅游会议详情") })
	@GetMapping(value = "/tourismMeeting", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public  ResponseEntity<?> findTourismMeeting(@RequestParam(value="id",required=true)@ApiParam(value="会议Id",required=true) String id){
		TourismMeetingEntity tourismMeeting = tourismMeetingSer.findById(id);
		TourismMeetingVo tourismMeetingVo = new TourismMeetingVo();
		BeanUtils.copyProperties(tourismMeeting, tourismMeetingVo);
		return GenericDataResponse.okWithData(tourismMeetingVo);
		
	}
	
	@ApiOperation(value = "对接旅游会议爬虫", notes = "对接旅游会议爬虫")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "对接旅游会议爬虫") })
	@PostMapping(value = "/tourismMeeting", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public  ResponseEntity<?> saveTourismMeeting(@RequestBody TourismMeetingVo tourismMeetingVo){
		
		TourismMeetingEntity tourismMeeting = new TourismMeetingEntity();
		BeanUtils.copyProperties(tourismMeetingVo, tourismMeeting);
		tourismMeeting.setId(idGenerator.nextId());
		tourismMeetingSer.save(tourismMeeting);
		return GenericResponse.ok();
		
	}

}
